import { createRoot } from "react-dom/client";
import XtxMessageBox from "./xtxMessageBox";
import { createElement } from "react";

const confirmDiv = document.createElement("div");

confirmDiv.setAttribute("class", "xtx-comfirm-container");

document.body.appendChild(confirmDiv);

export default ({ title, text }: { title: string; text: string }) => {
  return new Promise((resolve, reject) => {
    const onConfirm = function () {
      resolve(true);
      root.unmount();
    };
    const onCancel = function () {
      root.unmount();
      reject(new Error("cancel"));
    };

    const root = createRoot(confirmDiv);
    const vender = createElement(XtxMessageBox, {
      title,
      text,
      onConfirm,
      onCancel,
    });
    root.render(vender);
  });
};
